#include <stdlib.h>
#include <rtthread.h>

#define DBG_TAG "sensor_common.c"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#include <sensor_bh1750.h>
#include <sensor_fbm320.h>
#include <sensor_htu21d.h>
#include <sensor_mp503.h>
#include <sensor_common.h>
#include "led.h"

sensor_data_st sensor_data_t = { 0 };   // 读取到的传感器的数据
int temp_limit = 27;    // 温度的上限

/**
 * @brief  传感器处理线程
 * @param  parameter 线程的入口参数
 * @return 无
 */
void sersor_common_thread_entry(void *parameter)
{
    int i;

    pin_sensor_init();  // IO引脚传感器初始化
    bh1750_init();      // 光照度传感器初始化
    rt_thread_mdelay(50);
    fbm320_init();      // 大气压力传感器初始化
    rt_thread_mdelay(50);
    htu21d_init();      // 温湿度传感器初始化
    rt_thread_mdelay(50);
    adc_init();         // 空气质量传感器初始化
    rt_thread_mdelay(300);

    while (1)
    {
        pin_sensor_read(&sensor_data_t.pin_sensor_sta);
        sensor_data_t.bh1750_data = bh1750_get_data();  // 光照强度
        rt_thread_mdelay(200);

        fbm320_read_data(&sensor_data_t.fbm320_temp, &sensor_data_t.fbm320_pressure);   // 大气压
        rt_thread_mdelay(200);

        sensor_data_t.hut21d_temp = hut21d_read_temp(); // 温度
//        if((int)sensor_data_t.hut21d_temp * 10 > temp_limit * 10)
//        {
//            fan_on();
//        }
//        else
//        {
//            fan_off();
//        }

        sensor_data_t.hut21d_humi = hut21d_read_humi(); // 湿度
        rt_thread_mdelay(200);

        sensor_data_t.mp503_data = adc_getVal(AIR_ADC_CHANNEL); // 空气质量
        rt_thread_mdelay(200);

        // 打印获取到的传感器的状态值，使用变量 i 来控制打印的频率
        if(++i > 10)
        {
            i = 0;
            LOG_D("  Fan status = %d. 1 is on, 0 is off", sensor_data_t.pin_sensor_sta.fan);
            LOG_D("Flame status = %d. 1 is on, 0 is off", sensor_data_t.pin_sensor_sta.flame);
            LOG_D("Light = %d.%d lux", (int)sensor_data_t.bh1750_data, ((int)(sensor_data_t.bh1750_data*10)) % 10);
            LOG_D("Pressure = %d.%d hPa", (int)sensor_data_t.fbm320_pressure, ((int)(sensor_data_t.fbm320_pressure*10)) % 10);
            LOG_D("Temperature = %d.%d", (int)sensor_data_t.hut21d_temp, ((int)(sensor_data_t.hut21d_temp*10)) % 10);
            LOG_D("Humidity = %d.%d", (int)sensor_data_t.hut21d_humi, ((int)(sensor_data_t.hut21d_humi*10)) % 10);
            LOG_D("AirQuality = %d", (int)sensor_data_t.mp503_data);
        }
    }
}

/**
 * @brief  启动传感器处理线程
 * @param  无
 * @return 无
 */
int start_sersor_common_thread(void)
{
    /* 创建心跳灯处理线程 */
    rt_thread_t thread = rt_thread_create("sersor_common_thread", sersor_common_thread_entry, RT_NULL, 1024 * 8, 20,
            20);
    if (thread != RT_NULL)
    {
        rt_thread_startup(thread);  // 创建成功则启动线程
    }
    else
    {
        LOG_E("create sersor_common_thread failed");
        return (-RT_ERROR);
    }

    return RT_EOK;
}

/**
 * @brief  使用命令行设置温度的上限值
 * @param  argc 参数的个数
 * @param  argv 参数的内容
 * @return 无
 */
void temp_limit_set(int argc, char **argv)
{
    if(argc != 2)   // 判断参数的个数
    {
        LOG_E("input error");
        return;
    }

    int temp = atoi(argv[1]);
    if((temp >= 0) || (temp <= 100))
    {
        temp_limit = temp;
        LOG_I("temp limit is %d\n", temp_limit);
    }
}
MSH_CMD_EXPORT(temp_limit_set, temp_limit_set);

/**
 * @brief  打印温度的上限值
 * @param  无
 * @return 无
 */
void print_temp_limit(void)
{
    LOG_I("temp limit is %d\n", temp_limit);
}
MSH_CMD_EXPORT(print_temp_limit, print_temp_limit);
